package order.servlet;

import order.dao.customerDao;
import order.dao.foodDao;
import order.dao.managerDao;
import order.dao.storeDao;
import order.pojo.food_information;
import order.pojo.store_information;
import order.utils.DbUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

public class selectManager extends HttpServlet {
    DbUtil dbUtil = new DbUtil();
    storeDao storeDao=new storeDao();
    customerDao customerDao=new customerDao();
    foodDao foodDao=new foodDao();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("=>进入了get请求");
        HttpSession session = req.getSession();

        String action = req.getParameter("action");
        if("querry".equals(action)) {
            querryAllStore(req,resp);
            return;
        } else if("jum".equals(action)){
            jumJsp(req,resp);
            return;
        } else if("selectfood".equals(action)){
selectFoodByPublisher(req,resp);
            return;
        } else if("orderjsp".equals(action)){
            orderJsp(req, resp);
            return;
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        HttpSession session = req.getSession();

        String action = req.getParameter("action");
        if("querry".equals(action)) {
            querryAllStore(req,resp);
            return;
        } else if("jum".equals(action)){
          jumJsp(req,resp);
            return;
        } else if("selectfood".equals(action)){
            selectFoodByPublisher(req,resp);
            return;
        } else if("orderjsp".equals(action)){
             orderJsp(req, resp);
            return;
        }
    }
private void jumJsp(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {

    req.getRequestDispatcher("customer/selectManager.jsp").forward(req,resp);
}
    //显示商家信息
    private void querryAllStore(HttpServletRequest req,HttpServletResponse resp){
        Connection con = null;
        store_information store=new store_information();
         String message=null;
        try {

            con = dbUtil.getCon();
            String publisher=customerDao.querryPublisher(con);
            if("1".equals(publisher)){
                store_information store_information=new store_information();
                storeDao storeDao=new storeDao();
                store=storeDao.selectStoreById(con,store);
                req.setAttribute("store",store);
                req.setAttribute("message",message);
                req.getRequestDispatcher("customer/selectManager.jsp").forward(req,resp);

            }else{
                message="对不起，附近尚未有店家发布信息";
                req.setAttribute("message",message);
                resp.sendRedirect("customer/selectManager.jsp");

            }





        } catch (Exception e) {

        } finally {
            try {
                dbUtil.closeCon(con);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


    }
    //显示发布的菜品信息
    private void selectFoodByPublisher(HttpServletRequest req,HttpServletResponse resp){
        Connection con = null;
       food_information food=new food_information();
        String message=null;
        try {
            List<food_information> list = new ArrayList<food_information>();

            con = dbUtil.getCon();
            list=foodDao.selectFoodByPublisher(con);

            if(list!=null){

                req.setAttribute("foodlist",list);
                req.setAttribute("message",message);
                req.getRequestDispatcher("customer/index.jsp").forward(req,resp);

            }else{
                message="对不起，该店面尚未发布菜品信息！";
                req.setAttribute("message",message);
                resp.sendRedirect("customer/selectManager.jsp");

            }





        } catch (Exception e) {

        } finally {
            try {
                dbUtil.closeCon(con);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


    }
    private void orderJsp(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {

        req.getRequestDispatcher("customer/order.jsp").forward(req,resp);
    }





}
